package com.travel.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.group.common.PageResult;
import com.travel.group.entity.Group;
import com.travel.group.entity.GroupWxuser;
import com.travel.group.entity.Users;
import com.travel.group.entity.WxUsers;
import com.travel.group.service.GroupService;
import com.travel.group.service.GroupWxuserService;
import com.travel.group.service.WxUserService;
import com.travel.group.utils.WeChatResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

@Controller
public class GroupController {

    @Autowired
    private GroupService groupService;

    @Autowired
    private GroupWxuserService groupWxuserService;

    @Resource
    private WxUserService wxUserService;

    @Resource
    private GroupWxuserService wxuserService;

    @RequestMapping("/group/toPage")
    public String toPage(){
        return "group/list";
    }

    @RequestMapping("/wx/addGroupInfo")
    @ResponseBody
    public Object addGroupInfo(@RequestBody Group group){
        group.setStatus(0);
        if (groupService.save(group))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }

    @RequestMapping("/wx/getAllGroupInfo")
    @ResponseBody
    public Object getAllGroupInfo(HttpServletRequest request){
        String userId = request.getParameter("userId"); //除了自己发布的信息以外的信息
        QueryWrapper<Group> wrapper = new QueryWrapper<>();
        wrapper.ne("publisher", userId);
        List<Group> list = groupService.list(wrapper);

        for (Group group : list){
            QueryWrapper<GroupWxuser> groupWxuserWrapper = new QueryWrapper<>();
            groupWxuserWrapper.eq("group_id", group.getId());
            groupWxuserWrapper.eq("status", 1);
            List<GroupWxuser> groupWxuserList = groupWxuserService.list(groupWxuserWrapper);
            List<WxUsers> list1 = new LinkedList<>();
            for (GroupWxuser groupWxuser: groupWxuserList) {
                QueryWrapper<WxUsers> wxUsersQueryWrapper = new QueryWrapper<>();
                wxUsersQueryWrapper.eq("user_id", groupWxuser.getWxuserId());

                List<WxUsers> wxUsersList =  wxUserService.list(wxUsersQueryWrapper);
//                wxuserService.list(wxUsersQueryWrapper);
                list1.add(wxUsersList.get(0));
            }
            group.setNumber(groupWxuserList.size());
            group.setWxUsersList(list1);
        }
        return WeChatResponseUtil.ok(list);
    }

    @RequestMapping("/wx/getAllMyGroupInfo")
    @ResponseBody
    public Object getAllMyGroupInfo(HttpServletRequest request){
        String userId = request.getParameter("userId");
        QueryWrapper<Group> wrapper = new QueryWrapper<>();
        wrapper.eq("publisher", userId);
        List<Group> list = groupService.list(wrapper);

        for (Group group : list){
            QueryWrapper<GroupWxuser> groupWxuserWrapper = new QueryWrapper<>();
            groupWxuserWrapper.eq("group_id", group.getId());
            groupWxuserWrapper.eq("status", 1);
            List<GroupWxuser> groupWxuserList = groupWxuserService.list(groupWxuserWrapper);
            List<WxUsers> list1 = new LinkedList<>();
            for (GroupWxuser groupWxuser: groupWxuserList) {
                QueryWrapper<WxUsers> wxUsersQueryWrapper = new QueryWrapper<>();
                wxUsersQueryWrapper.eq("user_id", groupWxuser.getWxuserId());

                List<WxUsers> wxUsersList =  wxUserService.list(wxUsersQueryWrapper);
//                wxuserService.list(wxUsersQueryWrapper);
                list1.add(wxUsersList.get(0));
            }
            group.setNumber(groupWxuserList.size());
            group.setWxUsersList(list1);
        }
        return WeChatResponseUtil.ok(list);
    }

    //单独查询所用的接口
    @RequestMapping("/group/queryList")
    @ResponseBody
    public PageResult<Group> queryList(Integer page, Integer limit, @RequestParam("name") String name) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<Group> allArticle = groupService.getAllGroupInfoPlus(page, limit, name);
        Integer count = allArticle.size();
        PageResult<Group> res = new PageResult<>(allArticle, count);
        return res;
    }

    @RequestMapping("/group/query")
    @ResponseBody
    public PageResult<Group> list(Integer page, Integer limit) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<Group> allArticle = groupService.getAllGroupInfo(page, limit);
        Integer count = groupService.countData();
        PageResult<Group> res = new PageResult<>(allArticle, count);
        return res;
    }

    @RequestMapping("/group/delete/{id}")
    @ResponseBody
    public PageResult delete(@PathVariable("id") Integer id){
        if(groupService.removeById(id)){
            return new PageResult("删除成功",200);
        }
        return new PageResult("删除失败",400);
    }

    @RequestMapping("/group/check/{id}")
    @ResponseBody
    public PageResult check(@PathVariable("id") Integer id){
        Group group = groupService.getById(id);
        group.setStatus(1);
        if(groupService.updateById(group)){
            return new PageResult("审核成功",200);
        }
        return new PageResult("审核失败",400);
    }

    //同意用户是否参加该活动
    @RequestMapping("/wx/agreeGroup")
    @ResponseBody
    public PageResult agreeGroup(@RequestBody GroupWxuser wxuser){
        QueryWrapper<GroupWxuser> groupQueryWrapper = new QueryWrapper<>();
        groupQueryWrapper.eq("id", wxuser.getId());
        GroupWxuser group1 = new GroupWxuser();
        group1.setStatus(wxuser.getStatus());

        if(wxuserService.update(group1, groupQueryWrapper)){
            return new PageResult("审核成功",200);
        }
        return new PageResult("审核失败",400);
    }

    //获取我们所有的申请信息
    @RequestMapping("/wx/getMyGroupApplyInfo")
    @ResponseBody
    public Object getMyGroupApplyInfo(HttpServletRequest request){
        String userId = request.getParameter("userId"); //除了自己发布的信息以外的信息
        QueryWrapper<Group> wrapper = new QueryWrapper<>();
        wrapper.eq("publisher", userId);
        wrapper.eq("status", 1);
        List<Group> list = groupService.list(wrapper);

        for (Group group : list){
            QueryWrapper<GroupWxuser> groupWxuserWrapper = new QueryWrapper<>();
            groupWxuserWrapper.eq("group_id", group.getId());
            groupWxuserWrapper.eq("status",0);
            List<GroupWxuser> groupWxuserList = groupWxuserService.list(groupWxuserWrapper);

            for (GroupWxuser groupWxuser : groupWxuserList) {
                QueryWrapper<WxUsers> groupWxuserQueryWrapper = new QueryWrapper<>();
                groupWxuserQueryWrapper.eq("user_id", groupWxuser.getWxuserId());
                WxUsers users = wxUserService.getOne(groupWxuserQueryWrapper);
                groupWxuser.setWxUsers(users);
            }

//            group.setList(groupWxuserList);
            group.setNumber(groupWxuserList.size());

        }
        return WeChatResponseUtil.ok(list);
    }
}
